#include "keypresseventhandler.h"

KeyPressEventHandler::KeyPressEventHandler(QObject *parent) : QObject(parent)
{

}
bool KeyPressEventHandler::eventFilter(QObject *watched, QEvent *event) {
    if (event->type() == QEvent::KeyPress) {
        QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
        bool isDecimalPoint = keyEvent->key() == Qt::Key_Period;
        if (keyEvent->text().toInt() != 0 || keyEvent->key() == Qt::Key_Period) {
            if (isDecimalPoint) {
                QLineEdit *lineEdit = static_cast<QLineEdit*>(watched);
                bool hasDecimalPoint = lineEdit->text().contains('.');
                if (hasDecimalPoint) {
                    return true; // 如果已经有小数点，则不允许再输入一个小数点
                }
            }
        }

    }
    return QObject::eventFilter(watched,event); // 对其他事件不进行过滤
}
